package com.kinghood.authoritycenter.controller.organization;


import com.kinghood.authoritycenter.common.model.dao.auth.UserBaseDO;
import com.kinghood.authoritycenter.common.model.dao.organization.OrganizationDO;
import com.kinghood.authoritycenter.common.model.dto.common.IdListReqDTO;
import com.kinghood.authoritycenter.common.model.dto.common.NameReqDTO;
import com.kinghood.authoritycenter.common.model.dto.organization.*;
import com.kinghood.authoritycenter.common.model.vo.organization.GetUserVO;
import com.kinghood.authoritycenter.common.model.vo.organization.OrganizationUserQueryVO;
import com.kinghood.authoritycenter.service.IOrganizationService;
import com.kinghood.authoritycenter.service.IUserBaseService;
import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.constant.CommonConstants;
import com.kinghood.sharding.model.PageResponseVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 组织
 *
 * @author zhangFanJun
 * @date 2023-12-06 10:42
 **/
@RequestMapping("/organization")
@Slf4j
@RequiredArgsConstructor
@RestController
public class OrganizationController {

    private final IOrganizationService organizationService;
    private final IUserBaseService userBaseService;

    /**
     * 组织修改
     *
     * @param in 组织信息
     * @return 组织
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-保存组织信息")
    @PostMapping("/addOrUpdateOrganization")
    public KingHoodResponseVO<OrganizationDO> AddOrUpdateOrganization(@RequestBody OrganizationUpdateReqDTO in) {
        if(Objects.isNull(in.getId())){
            OrganizationAddReqDTO reqDTO = new OrganizationAddReqDTO();
            BeanUtils.copyProperties(in,reqDTO);
            return KingHoodResponseVO.success(organizationService.addOrganization(reqDTO));
        }
        return KingHoodResponseVO.success(organizationService.updateOrganization(in));
    }

    /**
     * 新建组织
     *
     * @param in 组织信息
     * @return 组织
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-新建组织")
    @PostMapping("/addOrganization")
    public KingHoodResponseVO<OrganizationDO> addOrganization(@RequestBody OrganizationAddReqDTO in) {
        return KingHoodResponseVO.success(organizationService.addOrganization(in));
    }

    /**
     * 获取组织信息
     *
     * @param id 组织id
     * @return 组织
     * @author zfj
     * @date 2023/12/18
     */
    @GetMapping("/getOrganizationInfo/{id}")
    public KingHoodResponseVO<OrganizationDO> getOrganizationInfo(@PathVariable("id") Long id) {
        return KingHoodResponseVO.success(organizationService.getOrganizationInfo(id));
    }

    /**
     * 组织修改
     *
     * @param in 组织信息
     * @return 组织
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-组织修改")
    @PostMapping("/updateOrganization")
    public KingHoodResponseVO<OrganizationDO> updateOrganization(@RequestBody OrganizationUpdateReqDTO in) {
        return KingHoodResponseVO.success(organizationService.updateOrganization(in));
    }

    /**
     * 批量删除组织
     *
     * @param in 组织id
     * @return 是否删除成功
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-批量删除组织")
    @PostMapping("/deleteOrganizationBatch")
    public KingHoodResponseVO<Boolean> deleteOrganizationBatch(@RequestBody IdListReqDTO in) {
        return KingHoodResponseVO.success(organizationService.deleteOrganizationBatch(in.getIdList()));
    }

    /**
     * <p>下拉列</p>
     * 全量组织数据
     *
     * @param in 入参
     * @return api列表
     * @author zfj
     * @date 2023/12/19
     */
    @PostMapping("/getOrganizationList")
    public KingHoodResponseVO<List<OrganizationDO>> getOrganizationList(@RequestBody OrganizationGetListReqDTO in) {
        in.setStatus(CommonConstants.ENABLE);
        return KingHoodResponseVO.success(organizationService.getList(in));
    }

    /**
     * 全量组织数据，包括禁用
     *
     * @param in 入参
     * @return api列表
     * @author zfj
     * @date 2024/01/02
     */
    @PostMapping("/getAllOrganizationList")
    public KingHoodResponseVO<List<OrganizationDO>> getAllOrganizationList(@RequestBody OrganizationGetListReqDTO in) {
        return KingHoodResponseVO.success(organizationService.getList(in));
    }

    /**
     * 用户分页
     *
     * @param in 筛选条件
     * @return 用户
     * @author zfj
     * @date 2023/12/18
     */
    @PostMapping("/queryUserPage")
    public KingHoodResponseVO<PageResponseVO<OrganizationUserQueryVO>> queryUserPage(@RequestBody OrganizationQueryReqDTO in) {
        return KingHoodResponseVO.success(userBaseService.queryUserPage(in));
    }

    /**
     * 人员信息修改
     *
     * @param in 人员信息修改
     * @return 人员信息修改
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-保存人员信息")
    @PostMapping("/addOrUpdateUser")
    public KingHoodResponseVO<UserBaseDO> updateUser(@RequestBody AddUserReqDTO in) {
        if(Objects.isNull(in.getId())){
            return KingHoodResponseVO.success(userBaseService.addUser(in));
        }
        UpdateUserReqDTO update = new UpdateUserReqDTO();
        BeanUtils.copyProperties(in,update);
        return KingHoodResponseVO.success(userBaseService.updateUser(update));
    }

    /**
     * 新增人员
     *
     * @param in 人员信息
     * @return 人员信息
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-新增人员")
    @PostMapping("/addUser")
    public KingHoodResponseVO<UserBaseDO> addUser(@Valid @RequestBody AddUserReqDTO in) {

        return KingHoodResponseVO.success(userBaseService.addUser(in));
    }

    /**
     * 人员信息获取
     *
     * @param userId 人员id
     * @return 人员信息
     * @author zfj
     * @date 2023/12/18
     */
    @GetMapping("/getUser/{userId}")
    public KingHoodResponseVO<GetUserVO> getUser(@PathVariable("userId") Long userId) {

        GetUserVO user = userBaseService.getUser(userId);
        user.setPassword(null);
        return KingHoodResponseVO.success(user);
    }

    /**
     * 人员信息修改
     *
     * @param in 人员信息修改
     * @return 人员信息修改
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-人员信息修改")
    @PostMapping("/updateUser")
    public KingHoodResponseVO<UserBaseDO> updateUser(@Valid @RequestBody UpdateUserReqDTO in) {
        return KingHoodResponseVO.success(userBaseService.updateUser(in));
    }

    /**
     * 人员删除
     *
     * @param userId 人员id
     * @return 人员信息修改
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-人员删除")
    @PostMapping("/deleteUser/{userId}")
    public KingHoodResponseVO<Boolean> deleteUser(@PathVariable("userId") Long userId) {
        return KingHoodResponseVO.success(userBaseService.deleteUser(Collections.singletonList(userId)));
    }

    /**
     * 人员删除
     *
     * @param in 人员id
     * @return 人员信息修改
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ORGANIZATIONAL_STRUCTURE,content = "组织架构-人员删除")
    @PostMapping("/deleteUserBatch")
    public KingHoodResponseVO<Boolean> deleteUserBatch(@RequestBody IdListReqDTO in) {
        return KingHoodResponseVO.success(userBaseService.deleteUser(in.getIdList()));
    }

    /**
     * <p>下拉列</p>
     * 获取人员
     *
     * @param in 入参
     * @return 人员
     * @author zfj
     * @date 2023/12/19
     */
    @PostMapping("/getUserList")
    public KingHoodResponseVO<List<UserBaseDO>> getList(@RequestBody NameReqDTO in) {

        return KingHoodResponseVO.success(userBaseService.getList(in.getName()));
    }

}
